﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HotelManagement._04.OBJECT;
using HotelManagement._02.BUS;
using HotelManagement;
using CrystalDecisions.Shared;
using CSDL_QuanLyBanHang._01.FORM;

namespace HotelManagement._01.FORM
{
    public partial class F52_Payment : Form
    {
        public F52_Payment()
        {
            
            InitializeComponent();
            Init();
            LoadData();
            //DateTime a = DateTime.Now;
            //string  b = DateTime.Now.
        }
        string m_InvoidNumber = "";
        Payment m_Payment;
        PaymentBUS m_PaymentBUS;
        CheckOutBUS m_CheckOutBUS;

        public void Init()
        {
            m_Payment = new Payment();
            m_PaymentBUS = new PaymentBUS();
            m_CheckOutBUS = new CheckOutBUS();
            
        }
        public void LoadData()
        {
            m_dgv_Payment.DataSource = m_PaymentBUS.ViewPayment();
        }

        private void m_dgv_CheckOut_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                m_Payment.CheckInID = int.Parse(m_dgv_Payment.Rows[m_dgv_Payment.CurrentCell.RowIndex].Cells["CheckInID"].Value.ToString());
                m_time_CheckOutDate.Text = (m_dgv_Payment.Rows[m_dgv_Payment.CurrentCell.RowIndex].Cells["CheckOutDate"].Value.ToString());
                m_Payment.CustomerID = int.Parse(m_dgv_Payment.Rows[m_dgv_Payment.CurrentCell.RowIndex].Cells["CustomerID"].Value.ToString());
                m_Payment.DepositCost = int.Parse(m_dgv_Payment.Rows[m_dgv_Payment.CurrentCell.RowIndex].Cells["DepositCost"].Value.ToString());
                m_txt_InvoidNumber.Text = m_PaymentBUS.CheckInvoidNumber().ToString();
                m_InvoidNumber = m_txt_InvoidNumber.Text;
                int RoomCost = 0;
                if (m_dgv_Payment.Rows[m_dgv_Payment.CurrentCell.RowIndex].Cells["RentCost"].Value.ToString() != "")
                {
                    RoomCost = int.Parse(m_dgv_Payment.Rows[m_dgv_Payment.CurrentCell.RowIndex].Cells["RentCost"].Value.ToString());
                }
                m_txt_RentCost.Text = RoomCost.ToString();
                //tính tiến phạt
                int Fines = 0;
                if ((m_dgv_Payment.Rows[m_dgv_Payment.CurrentCell.RowIndex].Cells["Fines"].Value.ToString()) != "")
                {
                    Fines = int.Parse(m_dgv_Payment.Rows[m_dgv_Payment.CurrentCell.RowIndex].Cells["Fines"].Value.ToString());
                }
                m_txt_Change.Text = Fines.ToString();

                m_Payment.Note = "";
                m_Payment.CheckOutID = int.Parse(m_dgv_Payment.Rows[m_dgv_Payment.CurrentCell.RowIndex].Cells["CheckOutID"].Value.ToString());
                if (m_Payment.CheckOutID == 0)
                {
                    m_txt_reason.Text = "";


                }
                if (m_Payment.CheckOutID == 1)
                {
                    m_txt_reason.Text = "Đổi";
                }
                if (m_Payment.CheckOutID == 2)
                {

                    m_txt_reason.Text = "Hủy";
                }
                // tiền dịch vụ

                DataTable v_dtService = m_PaymentBUS.ViewService(m_Payment.CustomerID);

                m_dvg_Service.DataSource = v_dtService;

                int serviceCost = 0;
                for (int i = 0; i < v_dtService.Rows.Count; i++)
                {
                    serviceCost = serviceCost + int.Parse(v_dtService.Rows[i]["ServiceCost"].ToString());

                }

                DataTable v_dt = m_CheckOutBUS.ViewInfoCustomer(m_Payment.CheckInID);


                m_txt_CustomerName.Text = v_dt.Rows[0][1].ToString();
                m_txt_IDCardNo.Text = v_dt.Rows[0][8].ToString();
                m_txt_Phone.Text = v_dt.Rows[0][6].ToString();
                m_txt_PassPort.Text = v_dt.Rows[0][9].ToString();
                m_txt_Address.Text = v_dt.Rows[0][5].ToString();


                //Tính giảm giá
                int discount = 0;
                if (m_dgv_Payment.Rows[m_dgv_Payment.CurrentCell.RowIndex].Cells["CheckInDate"].Value.ToString() != "")
                {
                    DateTime m_CheckInDate = DateTime.Parse(m_dgv_Payment.Rows[m_dgv_Payment.CurrentCell.RowIndex].Cells["CheckInDate"].Value.ToString());
                    discount = CheckDiscount(m_Payment.CustomerID, m_CheckInDate);
                }
                m_txt_discount.Text = discount.ToString();

                //tổng tiền
                m_Payment.TotalCost = (RoomCost + Fines + serviceCost) * (100 - discount) / 100 - m_Payment.DepositCost;
                m_txt_TotalCost.Text = m_Payment.TotalCost.ToString();

                m_txt_CostService.Text = serviceCost.ToString();

            }
            catch (Exception)
            {
                return;
            }
            
        }

        private void Reset()
        {
            m_txt_Address.Text = "";
            m_txt_Change.Text = "";
            m_txt_CostService.Text = "";
            m_txt_CustomerName.Text = "";
            m_txt_discount.Text = "";
            m_txt_IDCardNo.Text = "";
            m_txt_InvoidNumber.Text = "";
            m_txt_PassPort.Text = "";
            m_txt_Phone.Text = "";
            m_txt_reason.Text = "";
            m_txt_RentCost.Text = "";
            m_txt_TotalCost.Text = "";
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void m_btn_CheckOut_Click(object sender, EventArgs e)
        {
            
            m_Payment.InvoidNumber = m_txt_InvoidNumber.Text;

            if (m_PaymentBUS.PaymentCheckInUpdate(m_Payment))
            {
                DataTable v_dtService = m_PaymentBUS.ViewService(m_Payment.CustomerID);
                for (int i = 0; i < v_dtService.Rows.Count; i++)
                {
                    int CustomerServiceID = int.Parse(v_dtService.Rows[i]["CustomerServiceID"].ToString());
                    if (!m_PaymentBUS.UpdateCustomerService(CustomerServiceID))
                    {
                        MessageBox.Show("Có lỗi xảy ra");
                    }
                }
                    
                m_Payment.UserID = 1;
                m_Payment.PaymentDate = DateTime.Now;
                if (!m_PaymentBUS.InsertPayment(m_Payment))
                {
                    MessageBox.Show("Có lỗi xảy ra");
                }
                else
                {
                    MessageBox.Show("Thành công");
                    Reset();
                }

                LoadData();

            }

        }

        private int CheckDiscount(int customerId,DateTime dateCheckIn)
        {
            DataTable v_dt = m_CheckOutBUS.ViewInfoCustomer(m_Payment.CheckInID);
            DataTable v_dt1 = m_PaymentBUS.GetCheckInByCustomerIdAndDatetime(customerId, dateCheckIn);
            if (int.Parse(v_dt.Rows[0]["CustomerTypeID"].ToString()) == 1 && v_dt1.Rows.Count >= 3)
            {
                return 15; //25 %
            }
            if (int.Parse(v_dt.Rows[0]["CustomerTypeID"].ToString()) != 1 && v_dt1.Rows.Count >= 3)
            {
                return 10;
            }
            if (int.Parse(v_dt.Rows[0]["CustomerTypeID"].ToString()) == 1 && v_dt1.Rows.Count < 3)
            {
                return 5;
            }
            return 0;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            m_dgv_Payment.DataSource = m_PaymentBUS.SelectForPaymentByCustomerName(m_txt_CustomerName.Text.ToString());
        }

        private void bill_click(object sender, EventArgs e)
        {
            PaymentBUS v_PaymentBUS = new PaymentBUS();
            DataTable v_Dt = new DataTable();
            if (m_InvoidNumber == "")
            {
                MessageBox.Show("Phải thanh toán xong mới in hóa đơn ");
                return;
            }
            v_Dt = v_PaymentBUS.ViewBill(m_InvoidNumber);
            ParameterFields v_ParameterFields = new ParameterFields();
            //v_ParameterFields = v_ReportCustomerServiceBUS.AddParameter(m_dt_FromDate.Value, m_dt_ToDate.Value);
            F02_Report v_F02_Report = new F02_Report();
            v_F02_Report.Show(v_Dt, "Bill.rpt", v_ParameterFields);
        }
    }
}
